package btb.ui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import btb.engine.ATask;
import btb.engine.CEvent;
import btb.engine.CTaskMessenger;
import btb.net.CNetworkEvent;
import btb.util.CLogger;

public class CInputTask extends ATask implements KeyListener
{
	public CInputTask(int _taskId)
	{
		super(_taskId);
	}
	
	public CInputTask(ATask _parentTask, int _taskId)
	{
		super(_parentTask, _taskId);
	}
	
	public CInputTask(int _taskId, CTaskMessenger _mailbox)
	{
		super(_taskId, _mailbox);
	}
	
	public CInputTask(ATask _parentTask, int _taskId, CTaskMessenger _mailbox)
	{
		super(_parentTask, _taskId, _mailbox);
	}
	
	@Override
	public void keyTyped(KeyEvent e)
	{
		
	}
	
	@Override
	public void keyPressed(KeyEvent e)
	{
		
	}

	@Override
	public void keyReleased(KeyEvent e)
	{
		
	}

	@Override
	public void initialize()
	{
		System.out.println("Input task constructed and initialized.");
	}
	
	@Override
	public void update()
	{
		this.handleMail();
	}
	
	@Override
	public void handleEvent(CEvent _event)
	{
		int pos = 0;
		
		if(CIOEvent.class.isAssignableFrom(_event.getClass()))
		{	
			String header, body;
			CIOEvent cioe = (CIOEvent) _event;
			
			switch(_event.getEventType())
			{
			case CIOEvent.CIOE_GUI_EVENT:
				System.out.println("GUI event received: " + _event.getEventPayload());
				
				String payload = cioe.getEventPayload();
				pos = payload.indexOf('%');
				header = payload.substring(0, pos);
				body = payload.substring(pos, payload.length());
				
				if(header.equals("login"))	
					handleLoginAttempt(body);
				else if(header.equals("playoffline"))
					handleOfflinePlayRequest(body);
				else
					;
				
				break;
			case CIOEvent.CIOE_UNDEFINED_EVENT:
				System.out.println("Undefined IO event received.");
				break;
			default:
				System.out.println("Unknown event received.");
				break;
			}
		}
		else
		{
			
		}
	}
	
	@Override
	public void handleDeath(){}
	
	public void handleLoginAttempt(String _body)
	{
		int pos1, pos2;
		String logininfo, username, password;
		
		logininfo = _body.substring(_body.indexOf('%') + 1);
		pos1 = logininfo.indexOf('%');
		
		if(pos1 > -1)
		{
			username = logininfo.substring(0, pos1);
		
			if(username.length() > 0 && logininfo.length() > username.length() + 1)
			{
				pos2 = logininfo.indexOf('%', pos1 + 1);
				
				if(pos1 > -1) 
					password = logininfo.substring(pos1 + 1, pos2);
				else
					password = "";
			}
			else
				password = "";
		}
		else
		{
			username = password = "";
		}
		
		if(username.length() > 0 && password.length() > 0)
		{
			String networkEventString = username + "%" + password + "%";
			CNetworkEvent networkEvent = new CNetworkEvent(this, CNetworkEvent.CNE_LOGIN_EVENT, networkEventString);
			CTaskMessenger mBox = this.getMailbox();
			mBox.push(CTaskMessenger.TASK_MCLIENT, networkEvent);
		}
		else
		{
			// [Refactoring]: Add code that actually handles a zero-length username or password string, even though neither should reach this method.
			CLogger.cond_error("ui/input_task", "handleLoginAttempt() - This shouldn't happen. Code in the login gui page should prevent it.");
		}
	}
	
	public void handleOfflinePlayRequest(String _body)
	{
		// [Refactoring]: Later. Muuuuuch later.
	}
}
